package com.westcom.healthmate.controller;

import com.westcom.healthmate.bean.FramewareInfoBean;
import com.westcom.healthmate.bean.ResponseBean;
import com.westcom.healthmate.common.constants.ErrorConstants;
import com.westcom.healthmate.common.exception.ErrorException;
import com.westcom.healthmate.common.utils.CommonUtils;
import com.westcom.healthmate.service.FramewareService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

@RestController()
public class FramewareController extends BasicController
{
    @Autowired
    FramewareService fs;
    private static final Logger logger = LoggerFactory.getLogger(FramewareController.class);

    @RequestMapping(path="frameware/download/{versionCode}",method={RequestMethod.GET})
    public ResponseBean downloadFrameware(HttpServletRequest req, HttpServletResponse resp,
                                         @PathVariable("versionCode") int versionCode)
    {
        try
        {
            FramewareInfoBean info = fs.getFramewareInfo(versionCode);
            if(null == info)
            {
                return CommonUtils.getResponse(ErrorConstants.FRAMEWARE_NOT_FOUND,null,resp);
            }
            File f = new File(info.getPath());
            if(!f.exists())
            {
                return CommonUtils.getResponse(ErrorConstants.FRAMEWARE_NOT_FOUND,null,resp);
            }
            resp.setHeader("Content-MD5",CommonUtils.getFileMD5(info.getPath()));
            resp.setHeader("Version-Code",""+info.getVersionCode());
            resp.setHeader("Version-Name",info.getVersionName());
            resp.setContentType("multipart/form-data");
            resp.setHeader("Content-Disposition", "attachment;fileName="+f.getName());
            resp.setHeader("Release-Time",""+info.getReleaseTime());
            byte[] frameware = CommonUtils.readFile(info.getPath());
            resp.setContentLengthLong(frameware.length);
            OutputStream os = null;
            try
            {
                os = resp.getOutputStream();
                os.write(frameware);
                return null;
            } catch (IOException e)
            {
                e.printStackTrace();
                return CommonUtils.getResponse(ErrorConstants.FRAMEWARE_OPERATION_FAILED,null,resp);
            }finally
            {
                if(null != os)
                {
                    try
                    {
                        os.close();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                        return CommonUtils.getResponse(ErrorConstants.FRAMEWARE_OPERATION_FAILED,null,resp);
                    }
                }
            }
        } catch (ErrorException e)
        {
            e.printStackTrace();
            logger.error("get IAP info failed,version_code:{}",versionCode);
            return CommonUtils.getResponse(e.getCode(),null,resp);
        }
    }

    @RequestMapping(path="frameware/infos/all",method={RequestMethod.GET})
    public ResponseBean allFramewareInfos(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            return CommonUtils.getResponse(ErrorConstants.SUCCESS,fs.getAllFramewareInfos(),resp);
        } catch (Exception e)
        {
            e.printStackTrace();
            return CommonUtils.getResponse(ErrorConstants.FRAMEWARE_NOT_FOUND,null,resp);
        }
    }
}
